﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.MyLinq;
using Huirui.Cavan.Core.Persistent;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule
{
    public class PaySpecification : ISpecification<Pay, PaySearch>
    {
        #region ISpecification<Pay,PaySearch> 成员

        public PaySearch SearchParams
        {
            get;
            set;
        }

        #endregion

        #region ISpecification<Pay> 成员

        public System.Linq.Expressions.Expression<Func<Pay, bool>> Expression
        {
            get
            {
                IQueryBuilder<Pay> queryBuilder = QueryBuilder.Create<Pay>();

                if (SearchParams.IsNotNull())
                {
                    if (SearchParams.ID.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(p => p.Id, SearchParams.ID);
                    }
                    if (SearchParams.StartDate.HasValue && SearchParams.EndDate.HasValue)
                    {
                        queryBuilder = queryBuilder.Greater(p => p.CreateDate, SearchParams.StartDate.Value);
                        queryBuilder = queryBuilder.Less(p => p.CreateDate, SearchParams.EndDate.Value);
                    }
                    if (SearchParams.SupplierCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(p => p.Supplier.Id, SearchParams.SupplierCode);
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }

        #endregion
    }
}
